﻿package mortal.common.preLoadPage
{
    import Message.Login.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;

    public class SelectRoleView extends Sprite
    {
        private var _bmpTitle:GImageBitmap;
        private var _tileList:GTileList;
        private var _leftBtn:GLoadedButton;
        private var _rightBtn:GLoadedButton;
        private var _btnEnter:GLoadingButton;
        private var _page:int = 1;
        private var _maxPage:int = 1;

        public function SelectRoleView()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }// end function

        protected function onAddToStage(event:Event) : void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            this.init();
            return;
        }// end function

        protected function init() : void
        {
            this._bmpTitle = UIFactory.gImageBitmap(ImagesConst.hefuTextSelect, 0, 0, this);
            this._tileList = UIFactory.tileList(0, 45, 590, 300, this);
            this._tileList.columnWidth = 196;
            this._tileList.rowHeight = 300;
            this._tileList.horizontalGap = 0;
            this._tileList.verticalGap = 0;
            this._tileList.setStyle("cellRenderer", SelectRoleCellRenderer);
            this._tileList.direction = ScrollBarDirection.HORIZONTAL;
            this._tileList.verticalScrollPolicy = ScrollPolicy.OFF;
            this._tileList.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._tileList.x = 24;
            this._btnEnter = UIFactory.gLoadingButton(ResFileConst.hefuEnterBtn, 222, 385, 210, 67, this);
            this._leftBtn = UIFactory.gLoadedButton(ImagesConst.MountLeft_upSkin, 55, 355, 33, 37, this);
            this._rightBtn = UIFactory.gLoadedButton(ImagesConst.MountRight_upSkin, 550, 355, 33, 37, this);
            this.updateRoleList();
            this.onStageResize();
            Global.stage.addEventListener(Event.RESIZE, this.onStageResize);
            this._btnEnter.configEventListener(MouseEvent.CLICK, this.onClickBtn);
            this._leftBtn.configEventListener(MouseEvent.CLICK, this.onLeftBtn);
            this._rightBtn.configEventListener(MouseEvent.CLICK, this.onRightBtn);
            return;
        }// end function

        public function updateRoleList() : void
        {
            var _loc_3:SMyRole = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_1:* = PreloaderCache.myRoles;
            this._maxPage = Math.ceil(_loc_1.length / 3);
            var _loc_2:Boolean = false;
            for each (_loc_3 in _loc_1)
            {
                
                if (_loc_3.isMain == 1)
                {
                    _loc_2 = true;
                    break;
                }
            }
            _loc_1.sort(this.sortMyRole);
            _loc_4 = [];
            _loc_5 = (this._page - 1) * 3;
            _loc_6 = Math.min(this._page * 3, _loc_1.length);
            _loc_7 = _loc_5;
            while (_loc_7 < _loc_6)
            {
                
                _loc_4.push(_loc_1[_loc_7]);
                _loc_7++;
            }
            this._tileList.dataProvider = new DataProvider(_loc_4);
            this._tileList.selectedIndex = 0;
            this._tileList.x = 6 + (3 - this._tileList.dataProvider.length) * 196 / 2;
            if (_loc_2)
            {
                this._btnEnter.styleName = ResFileConst.hefuEnterBtn;
            }
            else
            {
                this._btnEnter.styleName = ResFileConst.hefuSelectMain;
            }
            this._leftBtn.visible = this._maxPage > 1;
            this._rightBtn.visible = this._maxPage > 1;
            this._leftBtn.filterEnabled = this._page > 1;
            this._rightBtn.filterEnabled = this._page < this._maxPage;
            return;
        }// end function

        protected function sortMyRole(param1:SMyRole, param2:SMyRole) : int
        {
            if (param1.isMain)
            {
                return -1;
            }
            if (param2.isMain)
            {
                return 1;
            }
            if (param1.level > param2.level)
            {
                return -1;
            }
            if (param1.level < param2.level)
            {
                return 1;
            }
            return 0;
        }// end function

        protected function onLeftBtn(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this._page - 1;
            _loc_2._page = _loc_3;
            this.updateRoleList();
            return;
        }// end function

        protected function onRightBtn(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this._page + 1;
            _loc_2._page = _loc_3;
            this.updateRoleList();
            return;
        }// end function

        protected function onClickBtn(event:MouseEvent) : void
        {
            if (this._btnEnter.styleName == ResFileConst.hefuEnterBtn)
            {
                this.onEnterBtnClick();
            }
            else
            {
                this.onSetMainClick();
            }
            return;
        }// end function

        private function onEnterBtnClick() : void
        {
            this.dispatchEvent(new DataEvent(EventName.HefuEnterGame, this._tileList.selectedItem));
            return;
        }// end function

        private function onSetMainClick() : void
        {
            SelectRoleAlertTipWindow.instance.show();
            SelectRoleAlertTipWindow.instance.setCloseCall(this.onSetMainCallback);
            SelectRoleAlertTipWindow.instance.updateTipRole(this._tileList.selectedItem as SMyRole);
            return;
        }// end function

        private function onSetMainCallback() : void
        {
            this.dispatchEvent(new DataEvent(EventName.HefuSetMain, this._tileList.selectedItem));
            return;
        }// end function

        protected function onStageResize(event:Event = null) : void
        {
            this.x = Global.stage.stageWidth / 2 - 300;
            this.y = Global.stage.stageHeight / 2 - 220;
            return;
        }// end function

    }
}
